1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.awt.windows;
27
28 import java.awt.Color;
29 import java.awt.Font;
30 import java.awt.Graphics2D;
31 import java.awt.GraphicsEnvironment;
32 import java.awt.HeadlessException;
33 import java.awt.KeyboardFocusManager;
34 import java.awt.Toolkit;
35 import java.awt.BasicStroke;
36 import java.awt.Button;
37 import java.awt.Component;
38 import java.awt.Dimension;
39 import java.awt.Event;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.awt.FileDialog;
43 import java.awt.Dialog;
44 import java.awt.Label;
45 import java.awt.Panel;
46 import java.awt.Rectangle;
47 import java.awt.Window;
48
49 import java.awt.image.BufferedImage;
50 import java.awt.image.IndexColorModel;
51
52 import java.awt.print.Pageable;
53 import java.awt.print.PageFormat;
54 import java.awt.print.Paper;
55 import java.awt.print.Printable;
56 import java.awt.print.PrinterJob;
57 import java.awt.print.PrinterException;
58 import javax.print.PrintService;
59
60 import java.io.IOException;
61 import java.io.File;
62
63 import java.util.Hashtable;
64 import java.util.Properties;
65 import java.util.MissingResourceException;
66 import java.util.ResourceBundle;
67
68 import sun.awt.Win32GraphicsEnvironment;
69
70 import sun.print.PeekGraphics;
71 import sun.print.PeekMetrics;
72
73 import java.net.URL;
74 import java.net.URI;
75 import java.net.URISyntaxException;
76
77 import javax.print.PrintServiceLookup;
78 import javax.print.attribute.PrintRequestAttributeSet;
79 import javax.print.attribute.HashPrintServiceAttributeSet;
80 import javax.print.attribute.HashPrintRequestAttributeSet;
81 import javax.print.attribute.Attribute;
82 import javax.print.attribute.standard.Sides;
83 import javax.print.attribute.standard.Chromaticity;
84 import javax.print.attribute.standard.PrintQuality;
85 import javax.print.attribute.standard.PrinterResolution;
86 import javax.print.attribute.standard.SheetCollate;
87 import javax.print.attribute.IntegerSyntax;
88 import javax.print.attribute.standard.Copies;
89 import javax.print.attribute.standard.Destination;
90 import javax.print.attribute.standard.OrientationRequested;
91 import javax.print.attribute.standard.Media;
92 import javax.print.attribute.standard.MediaSizeName;
93 import javax.print.attribute.standard.MediaSize;
94 import javax.print.attribute.standard.MediaTray;
95 import javax.print.attribute.standard.PrinterName;
96 import javax.print.attribute.standard.JobMediaSheetsSupported;
97 import javax.print.attribute.standard.PageRanges;
98 import javax.print.attribute.Size2DSyntax;
99 import javax.print.StreamPrintService;
100
101 import sun.awt.Win32FontManager;
102
103 import sun.print.RasterPrinterJob;
104 import sun.print.SunAlternateMedia;
105 import sun.print.SunPageSelection;
106 import sun.print.SunMinMaxPage;
107 import sun.print.Win32MediaTray;
108 import sun.print.Win32PrintService;
109 import sun.print.Win32PrintServiceLookup;
110 import sun.print.ServiceDialog;
111 import sun.print.DialogOwner;
112
113 import java.awt.Frame;
114 import java.io.FilePermission;
115
116 import sun.java2d.Disposer;
117 import sun.java2d.DisposerRecord;
118 import sun.java2d.DisposerTarget;
119
120
121
122
123
124
125 public class WPrinterJob extends RasterPrinterJob implements DisposerTarget {
126
127
128
129
130
131
132
133
134
135
136 protected static final long PS_ENDCAP_ROUND = 0x00000000;
137 protected static final long PS_ENDCAP_SQUARE = 0x00000100;
138 protected static final long PS_ENDCAP_FLAT = 0x00000200;
139
140
141
142
143
144 protected static final long PS_JOIN_ROUND = 0x00000000;
145 protected static final long PS_JOIN_BEVEL = 0x00001000;
146 protected static final long PS_JOIN_MITER = 0x00002000;
147
148
149
150
151
152
153
154
155 protected static final int POLYFILL_ALTERNATE = 1;
156
157
158
159
160
161
162
163
164 protected static final int POLYFILL_WINDING = 2;
165
166
167
168
169
170 private static final int MAX_WCOLOR = 255;
171
172
173
174
175
176 private static final int SET_DUP_VERTICAL = 0x00000010;
177 private static final int SET_DUP_HORIZONTAL = 0x00000020;
178 private static final int SET_RES_HIGH = 0x00000040;
179 private static final int SET_RES_LOW = 0x00000080;
180 private static final int SET_COLOR = 0x00000200;
181 private static final int SET_ORIENTATION = 0x00004000;
182
183
184
185
186 private static final int PD_ALLPAGES = 0x00000000;
187 private static final int PD_SELECTION = 0x00000001;
188 private static final int PD_PAGENUMS = 0x00000002;
189 private static final int PD_NOSELECTION = 0x00000004;
190 private static final int PD_COLLATE = 0x00000010;
191 private static final int PD_PRINTTOFILE = 0x00000020;
192 private static final int DM_ORIENTATION = 0x00000001;
193 private static final int DM_PRINTQUALITY = 0x00000400;
194 private static final int DM_COLOR = 0x00000800;
195 private static final int DM_DUPLEX = 0x00001000;
196
197
198
199
200 private static final int MAX_UNKNOWN_PAGES = 9999;
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 private boolean driverDoesMultipleCopies = false;
253 private boolean driverDoesCollation = false;
254 private boolean userRequestedCollation = false;
255 private boolean noDefaultPrinter = false;
256
257
258
259
260 static class HandleRecord implements DisposerRecord {
261
262
263
264
265
266
267
268
269
270 private long mPrintDC;
271 private long mPrintHDevMode;
272 private long mPrintHDevNames;
273
274 public void dispose() {
275 WPrinterJob.deleteDC(mPrintDC, mPrintHDevMode, mPrintHDevNames);
276 }
277 }
278
279 private HandleRecord handleRecord = new HandleRecord();
280
281 private int mPrintPaperSize;
282
283
284
285
286 private int mPrintXRes;
287
288 private int mPrintYRes;
289
290 private int mPrintPhysX;
291
292 private int mPrintPhysY;
293
294 private int mPrintWidth;
295
296 private int mPrintHeight;
297
298 private int mPageWidth;
299
300 private int mPageHeight;
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 private int mAttSides;
317 private int mAttChromaticity;
318 private int mAttXRes;
319 private int mAttYRes;
320 private int mAttQuality;
321 private int mAttCollate;
322 private int mAttCopies;
323 private int mAttMediaSizeName;
324 private int mAttMediaTray;
325
326 private String mDestination = null;
327
328
329
330
331
332 private Color mLastColor;
333
334
335
336
337
338 private Color mLastTextColor;
339
340
341
342
343
344
345 private String mLastFontFamily;
346 private float mLastFontSize;
347 private int mLastFontStyle;
348 private int mLastRotation;
349 private float mLastAwScale;
350
351
352 private PrinterJob pjob;
353
354 private java.awt.peer.ComponentPeer dialogOwnerPeer = null;
355
356
357
358 static {
359
360 Toolkit.getDefaultToolkit();
361
362 initIDs();
363
364 Win32FontManager.registerJREFontsForPrinting();
365 }
366
367
368
369 public WPrinterJob()
370 {
371 Disposer.addRecord(disposerReferent,
372 handleRecord = new HandleRecord());
373 initAttributeMembers();
374 }
375
376
377
378
379
380
381
382
383
384 private Object disposerReferent = new Object();
385
386 public Object getDisposerReferent() {
387 return disposerReferent;
388 }
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414 public PageFormat pageDialog(PageFormat page) throws HeadlessException {
415 if (GraphicsEnvironment.isHeadless()) {
416 throw new HeadlessException();
417 }
418
419 if (getPrintService() instanceof StreamPrintService) {
420 return super.pageDialog(page);
421 }
422
423 PageFormat pageClone = (PageFormat) page.clone();
424 boolean result = false;
425
426
427
428
429
430 WPageDialog dialog = new WPageDialog((Frame)null, this,
431 pageClone, null);
432 dialog.setRetVal(false);
433 dialog.setVisible(true);
434 result = dialog.getRetVal();
435 dialog.dispose();
436
437
438 if (result && (myService != null)) {
439
440
441 String printerName = getNativePrintService();
442 if (!myService.getName().equals(printerName)) {
443
444
445 try {
446 setPrintService(Win32PrintServiceLookup.
447 getWin32PrintLUS().
448 getPrintServiceByName(printerName));
449 } catch (PrinterException e) {
450 }
451 }
452
453
454 updatePageAttributes(myService, pageClone);
455
456 return pageClone;
457 } else {
458 return page;
459 }
460 }
461
462
463 private boolean displayNativeDialog() {
464
465 if (attributes == null) {
466 return false;
467 }
468
469 DialogOwner dlgOwner = (DialogOwner)attributes.get(DialogOwner.class);
470 Frame ownerFrame = (dlgOwner != null) ? dlgOwner.getOwner() : null;
471
472 WPrintDialog dialog = new WPrintDialog(ownerFrame, this);
473 dialog.setRetVal(false);
474 dialog.setVisible(true);
475 boolean prv = dialog.getRetVal();
476 dialog.dispose();
477
478 Destination dest =
479 (Destination)attributes.get(Destination.class);
480 if ((dest == null) || !prv){
481 return prv;
482 } else {
483 String title = null;
484 String strBundle = "sun.print.resources.serviceui";
485 ResourceBundle rb = ResourceBundle.getBundle(strBundle);
486 try {
487 title = rb.getString("dialog.printtofile");
488 } catch (MissingResourceException e) {
489 }
490 FileDialog fileDialog = new FileDialog(ownerFrame, title,
491 FileDialog.SAVE);
492
493 URI destURI = dest.getURI();
494
495
496 String pathName = (destURI != null) ?
497 destURI.getSchemeSpecificPart() : null;
498 if (pathName != null) {
499 File file = new File(pathName);
500 fileDialog.setFile(file.getName());
501 File parent = file.getParentFile();
502 if (parent != null) {
503 fileDialog.setDirectory(parent.getPath());
504 }
505 } else {
506 fileDialog.setFile("out.prn");
507 }
508
509 fileDialog.setVisible(true);
510 String fileName = fileDialog.getFile();
511 if (fileName == null) {
512 fileDialog.dispose();
513 return false;
514 }
515 String fullName = fileDialog.getDirectory() + fileName;
516 File f = new File(fullName);
517 File pFile = f.getParentFile();
518 while ((f.exists() &&
519 (!f.isFile() || !f.canWrite())) ||
520 ((pFile != null) &&
521 (!pFile.exists() || (pFile.exists() && !pFile.canWrite())))) {
522
523 (new PrintToFileErrorDialog(ownerFrame,
524 ServiceDialog.getMsg("dialog.owtitle"),
525 ServiceDialog.getMsg("dialog.writeerror")+" "+fullName,
526 ServiceDialog.getMsg("button.ok"))).setVisible(true);
527
528 fileDialog.setVisible(true);
529 fileName = fileDialog.getFile();
530 if (fileName == null) {
531 fileDialog.dispose();
532 return false;
533 }
534 fullName = fileDialog.getDirectory() + fileName;
535 f = new File(fullName);
536 pFile = f.getParentFile();
537 }
538 fileDialog.dispose();
539 attributes.add(new Destination(f.toURI()));
540 return true;
541 }
542
543 }
544
545
546
547
548
549
550
551
552
553
554 public boolean printDialog() throws HeadlessException {
555
556 if (GraphicsEnvironment.isHeadless()) {
557 throw new HeadlessException();
558 }
559
560
561 if (attributes == null) {
562 attributes = new HashPrintRequestAttributeSet();
563 }
564
565 if (getPrintService() instanceof StreamPrintService) {
566 return super.printDialog(attributes);
567 }
568
569 if (noDefaultPrinter == true) {
570 return false;
571 } else {
572 return displayNativeDialog();
573 }
574 }
575
576
577
578
579
580
581
582
583
584
585
586
587 public void setPrintService(PrintService service)
588 throws PrinterException {
589 super.setPrintService(service);
590 if (service instanceof StreamPrintService) {
591 return;
592 }
593 driverDoesMultipleCopies = false;
594 driverDoesCollation = false;
595 setNativePrintService(service.getName());
596 }
597
598
599 private native void setNativePrintService(String name)
600 throws PrinterException;
601
602 public PrintService getPrintService() {
603 if (myService == null) {
604 String printerName = getNativePrintService();
605
606 if (printerName != null) {
607 myService = Win32PrintServiceLookup.getWin32PrintLUS().
608 getPrintServiceByName(printerName);
609
610
611 if (myService != null) {
612 return myService;
613 }
614 }
615
616 myService = PrintServiceLookup.lookupDefaultPrintService();
617 if (myService != null) {
618 try {
619 setNativePrintService(myService.getName());
620 } catch (Exception e) {
621 myService = null;
622 }
623 }
624
625 }
626 return myService;
627 }
628
629 private native String getNativePrintService();
630
631 private void initAttributeMembers() {
632 mAttSides = 0;
633 mAttChromaticity = 0;
634 mAttXRes = 0;
635 mAttYRes = 0;
636 mAttQuality = 0;
637 mAttCollate = -1;
638 mAttCopies = 0;
639 mAttMediaTray = 0;
640 mAttMediaSizeName = 0;
641 mDestination = null;
642
643 }
644
645
646
647
648
649
650
651
652
653
654
655 protected void setAttributes(PrintRequestAttributeSet attributes)
656 throws PrinterException {
657
658
659 initAttributeMembers();
660 super.setAttributes(attributes);
661
662 mAttCopies = getCopiesInt();
663 mDestination = destinationAttr;
664
665 if (attributes == null) {
666 return;
667 }
668 Attribute[] attrs = attributes.toArray();
669 for (int i=0; i< attrs.length; i++) {
670 Attribute attr = attrs[i];
671 try {
672 if (attr.getCategory() == Sides.class) {
673 setSidesAttrib(attr);
674 }
675 else if (attr.getCategory() == Chromaticity.class) {
676 setColorAttrib(attr);
677 }
678 else if (attr.getCategory() == PrinterResolution.class) {
679 setResolutionAttrib(attr);
680 }
681 else if (attr.getCategory() == PrintQuality.class) {
682 setQualityAttrib(attr);
683 }
684 else if (attr.getCategory() == SheetCollate.class) {
685 setCollateAttrib(attr);
686 } else if (attr.getCategory() == Media.class ||
687 attr.getCategory() == SunAlternateMedia.class) {
688
689
690
691 if (attr.getCategory() == SunAlternateMedia.class) {
692 Media media = (Media)attributes.get(Media.class);
693 if (media == null ||
694 !(media instanceof MediaTray)) {
695 attr = ((SunAlternateMedia)attr).getMedia();
696 }
697 }
698 if (attr instanceof MediaSizeName) {
699 setWin32MediaAttrib(attr);
700 }
701 if (attr instanceof MediaTray) {
702 setMediaTrayAttrib(attr);
703 }
704 }
705
706 } catch (ClassCastException e) {
707 }
708 }
709 }
710
711
712
713
714
715 private native void getDefaultPage(PageFormat page);
716
717
718
719
720
721
722
723
724 public PageFormat defaultPage(PageFormat page) {
725 PageFormat newPage = (PageFormat)page.clone();
726 getDefaultPage(newPage);
727 return newPage;
728 }
729
730
731
732
733 protected native void validatePaper(Paper origPaper, Paper newPaper );
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748 protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
749 PrinterJob printerJob,
750 Printable painter,
751 PageFormat pageFormat,
752 int pageIndex) {
753
754 WPathGraphics pathGraphics;
755 PeekMetrics metrics = peekGraphics.getMetrics();
756
757
758
759
760
761
762
763
764 if (forcePDL == false && (forceRaster == true
765 || metrics.hasNonSolidColors()
766 || metrics.hasCompositing()
767 )) {
768 pathGraphics = null;
769 } else {
770 BufferedImage bufferedImage = new BufferedImage(8, 8,
771 BufferedImage.TYPE_INT_RGB);
772 Graphics2D bufferedGraphics = bufferedImage.createGraphics();
773
774 boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;
775 pathGraphics = new WPathGraphics(bufferedGraphics, printerJob,
776 painter, pageFormat, pageIndex,
777 canRedraw);
778 }
779
780 return pathGraphics;
781 }
782
783
784 protected double getXRes() {
785 if (mAttXRes != 0) {
786 return mAttXRes;
787 } else {
788 return mPrintXRes;
789 }
790 }
791
792 protected double getYRes() {
793 if (mAttYRes != 0) {
794 return mAttYRes;
795 } else {
796 return mPrintYRes;
797 }
798 }
799
800 protected double getPhysicalPrintableX(Paper p) {
801 return mPrintPhysX;
802 }
803
804 protected double getPhysicalPrintableY(Paper p) {
805 return mPrintPhysY;
806 }
807
808 protected double getPhysicalPrintableWidth(Paper p) {
809 return mPrintWidth;
810 }
811
812 protected double getPhysicalPrintableHeight(Paper p) {
813 return mPrintHeight;
814 }
815
816 protected double getPhysicalPageWidth(Paper p) {
817 return mPageWidth;
818 }
819
820 protected double getPhysicalPageHeight(Paper p) {
821 return mPageHeight;
822 }
823
824
825
826
827
828
829
830
831 protected boolean isCollated() {
832 return userRequestedCollation;
833 }
834
835
836
837
838
839
840
841
842
843 protected int getCollatedCopies() {
844 debug_println("driverDoesMultipleCopies="+driverDoesMultipleCopies
845 +" driverDoesCollation="+driverDoesCollation);
846 if (super.isCollated() && !driverDoesCollation) {
847
848
849 mAttCollate = 0;
850 mAttCopies = 1;
851 return getCopies();
852 }
853
854 return 1;
855 }
856
857
858
859
860
861
862
863
864
865 protected int getNoncollatedCopies() {
866 if (driverDoesMultipleCopies || super.isCollated()) {
867 return 1;
868 } else {
869 return getCopies();
870 }
871 }
872
873
874
875
876
877
878
879 private long getPrintDC() {
880 return handleRecord.mPrintDC;
881 }
882
883 private void setPrintDC(long mPrintDC) {
884 handleRecord.mPrintDC = mPrintDC;
885 }
886
887 private long getDevMode() {
888 return handleRecord.mPrintHDevMode;
889 }
890
891 private void setDevMode(long mPrintHDevMode) {
892 handleRecord.mPrintHDevMode = mPrintHDevMode;
893 }
894
895 private long getDevNames() {
896 return handleRecord.mPrintHDevNames;
897 }
898
899 private void setDevNames(long mPrintHDevNames) {
900 handleRecord.mPrintHDevNames = mPrintHDevNames;
901 }
902
903 protected void beginPath() {
904 beginPath(getPrintDC());
905 }
906
907 protected void endPath() {
908 endPath(getPrintDC());
909 }
910
911 protected void closeFigure() {
912 closeFigure(getPrintDC());
913 }
914
915 protected void fillPath() {
916 fillPath(getPrintDC());
917 }
918
919 protected void moveTo(float x, float y) {
920 moveTo(getPrintDC(), x, y);
921 }
922
923 protected void lineTo(float x, float y) {
924 lineTo(getPrintDC(), x, y);
925 }
926
927 protected void polyBezierTo(float control1x, float control1y,
928 float control2x, float control2y,
929 float endX, float endY) {
930
931 polyBezierTo(getPrintDC(), control1x, control1y,
932 control2x, control2y,
933 endX, endY);
934 }
935
936
937
938
939
940
941
942 protected void setPolyFillMode(int fillRule) {
943 setPolyFillMode(getPrintDC(), fillRule);
944 }
945
946
947
948
949
950
951
952 protected void selectSolidBrush(Color color) {
953
954
955
956 if (color.equals(mLastColor) == false) {
957 mLastColor = color;
958 float[] rgb = color.getRGBColorComponents(null);
959
960 selectSolidBrush(getPrintDC(),
961 (int) (rgb[0] * MAX_WCOLOR),
962 (int) (rgb[1] * MAX_WCOLOR),
963 (int) (rgb[2] * MAX_WCOLOR));
964 }
965 }
966
967
968
969
970
971 protected int getPenX() {
972
973 return getPenX(getPrintDC());
974 }
975
976
977
978
979
980
981 protected int getPenY() {
982
983 return getPenY(getPrintDC());
984 }
985
986
987
988
989
990 protected void selectClipPath() {
991 selectClipPath(getPrintDC());
992 }
993
994
995 protected void frameRect(float x, float y, float width, float height) {
996 frameRect(getPrintDC(), x, y, width, height);
997 }
998
999 protected void fillRect(float x, float y, float width, float height,
1000 Color color) {
1001 float[] rgb = color.getRGBColorComponents(null);
1002
1003 fillRect(getPrintDC(), x, y, width, height,
1004 (int) (rgb[0] * MAX_WCOLOR),
1005 (int) (rgb[1] * MAX_WCOLOR),
1006 (int) (rgb[2] * MAX_WCOLOR));
1007 }
1008
1009
1010 protected void selectPen(float width, Color color) {
1011
1012 float[] rgb = color.getRGBColorComponents(null);
1013
1014 selectPen(getPrintDC(), width,
1015 (int) (rgb[0] * MAX_WCOLOR),
1016 (int) (rgb[1] * MAX_WCOLOR),
1017 (int) (rgb[2] * MAX_WCOLOR));
1018 }
1019
1020
1021 protected boolean selectStylePen(int cap, int join, float width,
1022 Color color) {
1023
1024 long endCap;
1025 long lineJoin;
1026
1027 float[] rgb = color.getRGBColorComponents(null);
1028
1029 switch(cap) {
1030 case BasicStroke.CAP_BUTT: endCap = PS_ENDCAP_FLAT; break;
1031 case BasicStroke.CAP_ROUND: endCap = PS_ENDCAP_ROUND; break;
1032 default:
1033 case BasicStroke.CAP_SQUARE: endCap = PS_ENDCAP_SQUARE; break;
1034 }
1035
1036 switch(join) {
1037 case BasicStroke.JOIN_BEVEL:lineJoin = PS_JOIN_BEVEL; break;
1038 default:
1039 case BasicStroke.JOIN_MITER:lineJoin = PS_JOIN_MITER; break;
1040 case BasicStroke.JOIN_ROUND:lineJoin = PS_JOIN_ROUND; break;
1041 }
1042
1043 return (selectStylePen(getPrintDC(), endCap, lineJoin, width,
1044 (int) (rgb[0] * MAX_WCOLOR),
1045 (int) (rgb[1] * MAX_WCOLOR),
1046 (int) (rgb[2] * MAX_WCOLOR)));
1047 }
1048
1049
1050
1051
1052
1053 protected boolean setFont(String family, float size, int style,
1054 int rotation, float awScale) {
1055
1056 boolean didSetFont = true;
1057
1058 if (!family.equals(mLastFontFamily) ||
1059 size != mLastFontSize ||
1060 style != mLastFontStyle ||
1061 rotation != mLastRotation ||
1062 awScale != mLastAwScale) {
1063
1064 didSetFont = setFont(getPrintDC(),
1065 family,
1066 size,
1067 (style & Font.BOLD) != 0,
1068 (style & Font.ITALIC) != 0,
1069 rotation, awScale);
1070 if (didSetFont) {
1071 mLastFontFamily = family;
1072 mLastFontSize = size;
1073 mLastFontStyle = style;
1074 mLastRotation = rotation;
1075 mLastAwScale = awScale;
1076 }
1077 }
1078 return didSetFont;
1079 }
1080
1081
1082
1083
1084 protected void setTextColor(Color color) {
1085
1086
1087
1088 if (color.equals(mLastTextColor) == false) {
1089 mLastTextColor = color;
1090 float[] rgb = color.getRGBColorComponents(null);
1091
1092 setTextColor(getPrintDC(),
1093 (int) (rgb[0] * MAX_WCOLOR),
1094 (int) (rgb[1] * MAX_WCOLOR),
1095 (int) (rgb[2] * MAX_WCOLOR));
1096 }
1097 }
1098
1099
1100
1101
1102 protected String removeControlChars(String str) {
1103 return super.removeControlChars(str);
1104 }
1105
1106
1107
1108
1109
1110 protected void textOut(String str, float x, float y,
1111 float[] positions) {
1112
1113
1114
1115
1116
1117
1118 String text = removeControlChars(str);
1119 assert (positions == null) || (text.length() == str.length());
1120 if (text.length() == 0) {
1121 return;
1122 }
1123 textOut(getPrintDC(), text, text.length(), false, x, y, positions);
1124 }
1125
1126
1127
1128
1129
1130 protected void glyphsOut(int []glyphs, float x, float y,
1131 float[] positions) {
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141 char[] glyphCharArray = new char[glyphs.length];
1142 for (int i=0;i<glyphs.length;i++) {
1143 glyphCharArray[i] = (char)(glyphs[i] & 0xffff);
1144 }
1145 String glyphStr = new String(glyphCharArray);
1146 textOut(getPrintDC(), glyphStr, glyphs.length, true, x, y, positions);
1147 }
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 protected int getGDIAdvance(String text) {
1159
1160 text = removeControlChars(text);
1161 if (text.length() == 0) {
1162 return 0;
1163 }
1164 return getGDIAdvance(getPrintDC(), text);
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179 protected void drawImage3ByteBGR(byte[] image,
1180 float destX, float destY,
1181 float destWidth, float destHeight,
1182 float srcX, float srcY,
1183 float srcWidth, float srcHeight) {
1184
1185
1186 drawDIBImage(getPrintDC(), image,
1187 destX, destY,
1188 destWidth, destHeight,
1189 srcX, srcY,
1190 srcWidth, srcHeight,
1191 24, null);
1192
1193 }
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210 protected void drawDIBImage(byte[] image,
1211 float destX, float destY,
1212 float destWidth, float destHeight,
1213 float srcX, float srcY,
1214 float srcWidth, float srcHeight,
1215 int sampleBitsPerPixel,
1216 IndexColorModel icm) {
1217 int bitCount = 24;
1218 byte[] bmiColors = null;
1219
1220 if (icm != null) {
1221 bitCount = sampleBitsPerPixel;
1222 bmiColors = new byte[(1<<icm.getPixelSize())*4];
1223 for (int i=0;i<icm.getMapSize(); i++) {
1224 bmiColors[i*4+0]=(byte)(icm.getBlue(i)&0xff);
1225 bmiColors[i*4+1]=(byte)(icm.getGreen(i)&0xff);
1226 bmiColors[i*4+2]=(byte)(icm.getRed(i)&0xff);
1227 }
1228 }
1229
1230 drawDIBImage(getPrintDC(), image,
1231 destX, destY,
1232 destWidth, destHeight,
1233 srcX, srcY,
1234 srcWidth, srcHeight,
1235 bitCount, bmiColors);
1236 }
1237
1238
1239
1240
1241 protected void startPage(PageFormat format, Printable painter,
1242 int index, boolean paperChanged) {
1243
1244
1245
1246
1247
1248
1249 invalidateCachedState();
1250
1251 deviceStartPage(format, painter, index, paperChanged);
1252 }
1253
1254
1255
1256
1257 protected void endPage(PageFormat format, Printable painter,
1258 int index) {
1259
1260 deviceEndPage(format, painter, index);
1261 }
1262
1263
1264
1265
1266 private void invalidateCachedState() {
1267 mLastColor = null;
1268 mLastTextColor = null;
1269 mLastFontFamily = null;
1270 }
1271
1272
1273
1274
1275 public void setCopies(int copies) {
1276 super.setCopies(copies);
1277 mAttCopies = copies;
1278 setNativeCopies(copies);
1279 }
1280
1281
1282
1283
1284
1285
1286
1287 public native void setNativeCopies(int copies);
1288
1289
1290
1291
1292
1293
1294
1295 private native boolean jobSetup(Pageable doc, boolean allowPrintToFile);
1296
1297
1298
1299
1300 protected native void initPrinter();
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313 private native boolean _startDoc(String dest, String jobName)
1314 throws PrinterException;
1315 protected void startDoc() throws PrinterException {
1316 if (!_startDoc(mDestination, getJobName())) {
1317 cancel();
1318 }
1319 }
1320
1321
1322
1323
1324
1325 protected native void endDoc();
1326
1327
1328
1329
1330
1331 protected native void abortDoc();
1332
1333
1334
1335
1336 private static native void deleteDC(long dc, long devmode, long devnames);
1337
1338
1339
1340
1341
1342 protected native void deviceStartPage(PageFormat format, Printable painter,
1343 int index, boolean paperChanged);
1344
1345
1346
1347
1348 protected native void deviceEndPage(PageFormat format, Printable painter,
1349 int index);
1350
1351
1352
1353
1354
1355
1356
1357
1358 protected native void printBand(byte[] data, int x, int y,
1359 int width, int height);
1360
1361
1362
1363
1364
1365 protected native void beginPath(long printDC);
1366
1367
1368
1369
1370
1371 protected native void endPath(long printDC);
1372
1373
1374
1375
1376
1377 protected native void closeFigure(long printDC);
1378
1379
1380
1381
1382
1383 protected native void fillPath(long printDC);
1384
1385
1386
1387
1388
1389 protected native void moveTo(long printDC, float x, float y);
1390
1391
1392
1393
1394
1395 protected native void lineTo(long printDC, float x, float y);
1396
1397 protected native void polyBezierTo(long printDC,
1398 float control1x, float control1y,
1399 float control2x, float control2y,
1400 float endX, float endY);
1401
1402
1403
1404
1405
1406
1407
1408 protected native void setPolyFillMode(long printDC, int fillRule);
1409
1410
1411
1412
1413
1414
1415
1416 protected native void selectSolidBrush(long printDC,
1417 int red, int green, int blue);
1418
1419
1420
1421
1422
1423
1424 protected native int getPenX(long printDC);
1425
1426
1427
1428
1429
1430
1431 protected native int getPenY(long printDC);
1432
1433
1434
1435
1436
1437 protected native void selectClipPath(long printDC);
1438
1439
1440
1441
1442 protected native void frameRect(long printDC, float x, float y,
1443 float width, float height);
1444
1445
1446
1447
1448
1449 protected native void fillRect(long printDC, float x, float y,
1450 float width, float height,
1451 int red, int green, int blue);
1452
1453
1454
1455
1456
1457 protected native void selectPen(long printDC, float width,
1458 int red, int green, int blue);
1459
1460
1461
1462
1463
1464 protected native boolean selectStylePen(long printDC, long cap,
1465 long join, float width,
1466 int red, int green, int blue);
1467
1468
1469
1470
1471
1472 protected native boolean setFont(long printDC, String familyName,
1473 float fontSize,
1474 boolean bold,
1475 boolean italic,
1476 int rotation,
1477 float awScale);
1478
1479
1480
1481
1482
1483 protected native void setTextColor(long printDC,
1484 int red, int green, int blue);
1485
1486
1487
1488
1489
1490
1491
1492 protected native void textOut(long printDC, String text,
1493 int strlen, boolean glyphs,
1494 float x, float y, float[] positions);
1495
1496
1497 private native int getGDIAdvance(long printDC, String text);
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514 private native void drawDIBImage(long printDC, byte[] image,
1515 float destX, float destY,
1516 float destWidth, float destHeight,
1517 float srcX, float srcY,
1518 float srcWidth, float srcHeight,
1519 int bitCount, byte[] bmiColors);
1520
1521
1522
1523
1524 private final String getPrinterAttrib() {
1525
1526 PrintService service = this.getPrintService();
1527 String name = (service != null) ? service.getName() : null;
1528 return name;
1529 }
1530
1531
1532 private final boolean getCollateAttrib() {
1533 return (mAttCollate == 1);
1534 }
1535
1536 private void setCollateAttrib(Attribute attr) {
1537 if (attr == SheetCollate.COLLATED) {
1538 mAttCollate = 1;
1539 } else {
1540 mAttCollate = 0;
1541 }
1542 }
1543
1544 private void setCollateAttrib(Attribute attr,
1545 PrintRequestAttributeSet set) {
1546 setCollateAttrib(attr);
1547 set.add(attr);
1548 }
1549
1550
1551
1552 private final int getOrientAttrib() {
1553 int orient = PageFormat.PORTRAIT;
1554 OrientationRequested orientReq = (attributes == null) ? null :
1555 (OrientationRequested)attributes.get(OrientationRequested.class);
1556 if (orientReq != null) {
1557 if (orientReq == OrientationRequested.REVERSE_LANDSCAPE) {
1558 orient = PageFormat.REVERSE_LANDSCAPE;
1559 } else if (orientReq == OrientationRequested.LANDSCAPE) {
1560 orient = PageFormat.LANDSCAPE;
1561 }
1562 }
1563
1564 return orient;
1565 }
1566
1567 private void setOrientAttrib(Attribute attr,
1568 PrintRequestAttributeSet set) {
1569 if (set != null) {
1570 set.add(attr);
1571 }
1572 }
1573
1574
1575 private final int getCopiesAttrib() {
1576 return getCopiesInt();
1577 }
1578
1579 private final void setRangeCopiesAttribute(int from, int to,
1580 boolean isRangeSet,
1581 int copies) {
1582 if (attributes != null) {
1583 if (isRangeSet) {
1584 attributes.add(new PageRanges(from, to));
1585 setPageRange(from, to);
1586 }
1587 attributes.add(new Copies(copies));
1588
1589
1590
1591
1592 super.setCopies(copies);
1593 mAttCopies = copies;
1594 }
1595 }
1596
1597
1598 private final int getFromPageAttrib() {
1599 if (attributes != null) {
1600 PageRanges pageRangesAttr =
1601 (PageRanges)attributes.get(PageRanges.class);
1602 if (pageRangesAttr != null) {
1603 int[][] range = pageRangesAttr.getMembers();
1604 return range[0][0];
1605 }
1606 }
1607 return getMinPageAttrib();
1608 }
1609
1610
1611 private final int getToPageAttrib() {
1612 if (attributes != null) {
1613 PageRanges pageRangesAttr =
1614 (PageRanges)attributes.get(PageRanges.class);
1615 if (pageRangesAttr != null) {
1616 int[][] range = pageRangesAttr.getMembers();
1617 return range[range.length-1][1];
1618 }
1619 }
1620 return getMaxPageAttrib();
1621 }
1622
1623 private final int getMinPageAttrib() {
1624 if (attributes != null) {
1625 SunMinMaxPage s =
1626 (SunMinMaxPage)attributes.get(SunMinMaxPage.class);
1627 if (s != null) {
1628 return s.getMin();
1629 }
1630 }
1631 return 1;
1632 }
1633
1634 private final int getMaxPageAttrib() {
1635 if (attributes != null) {
1636 SunMinMaxPage s =
1637 (SunMinMaxPage)attributes.get(SunMinMaxPage.class);
1638 if (s != null) {
1639 return s.getMax();
1640 }
1641 }
1642
1643 Pageable pageable = getPageable();
1644 if (pageable != null) {
1645 int numPages = pageable.getNumberOfPages();
1646 if (numPages <= Pageable.UNKNOWN_NUMBER_OF_PAGES) {
1647 numPages = MAX_UNKNOWN_PAGES;
1648 }
1649 return ((numPages == 0) ? 1 : numPages);
1650 }
1651
1652 return Integer.MAX_VALUE;
1653 }
1654
1655 private final boolean getDestAttrib() {
1656 return (mDestination != null);
1657 }
1658
1659
1660 private final int getQualityAttrib() {
1661 return mAttQuality;
1662 }
1663
1664 private void setQualityAttrib(Attribute attr) {
1665 if (attr == PrintQuality.HIGH) {
1666 mAttQuality = -4;
1667 } else if (attr == PrintQuality.NORMAL) {
1668 mAttQuality = -3;
1669 } else {
1670 mAttQuality = -2;
1671 }
1672 }
1673
1674 private void setQualityAttrib(Attribute attr,
1675 PrintRequestAttributeSet set) {
1676 setQualityAttrib(attr);
1677 set.add(attr);
1678 }
1679
1680
1681 private final int getColorAttrib() {
1682 return mAttChromaticity;
1683 }
1684
1685 private void setColorAttrib(Attribute attr) {
1686 if (attr == Chromaticity.COLOR) {
1687 mAttChromaticity = 2;
1688 } else {
1689 mAttChromaticity = 1;
1690 }
1691 }
1692
1693 private void setColorAttrib(Attribute attr,
1694 PrintRequestAttributeSet set) {
1695 setColorAttrib(attr);
1696 set.add(attr);
1697 }
1698
1699
1700 private final int getSidesAttrib() {
1701 return mAttSides;
1702 }
1703
1704 private void setSidesAttrib(Attribute attr) {
1705 if (attr == Sides.TWO_SIDED_LONG_EDGE) {
1706 mAttSides = 2;
1707 } else if (attr == Sides.TWO_SIDED_SHORT_EDGE) {
1708 mAttSides = 3;
1709 } else {
1710 mAttSides = 1;
1711 }
1712 }
1713
1714 private void setSidesAttrib(Attribute attr,
1715 PrintRequestAttributeSet set) {
1716 setSidesAttrib(attr);
1717 set.add(attr);
1718 }
1719
1720
1721 private final int[] getWin32MediaAttrib() {
1722 int wid_ht[] = {0, 0};
1723 if (attributes != null) {
1724 Media media = (Media)attributes.get(Media.class);
1725 if (media instanceof MediaSizeName) {
1726 MediaSizeName msn = (MediaSizeName)media;
1727 MediaSize ms = MediaSize.getMediaSizeForName(msn);
1728 if (ms != null) {
1729 wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
1730 wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
1731 }
1732 }
1733 }
1734 return wid_ht;
1735 }
1736
1737 private void setWin32MediaAttrib(Attribute attr) {
1738 if (!(attr instanceof MediaSizeName)) {
1739 return;
1740 }
1741 MediaSizeName msn = (MediaSizeName)attr;
1742 mAttMediaSizeName = ((Win32PrintService)myService).findPaperID(msn);
1743 }
1744
1745 private void setWin32MediaAttrib(int dmIndex, int width, int length) {
1746 MediaSizeName msn =
1747 ((Win32PrintService)myService).findWin32Media(dmIndex);
1748 if (msn == null) {
1749 msn = ((Win32PrintService)myService).
1750 findMatchingMediaSizeNameMM((float)width, (float)length);
1751 }
1752
1753 if (msn != null) {
1754 if (attributes != null) {
1755 attributes.add(msn);
1756 }
1757 }
1758 mAttMediaSizeName = dmIndex;
1759 }
1760
1761
1762 private void setMediaTrayAttrib(Attribute attr) {
1763 if (attr == MediaTray.BOTTOM) {
1764 mAttMediaTray = 2;
1765 } else if (attr == MediaTray.ENVELOPE) {
1766 mAttMediaTray = 5;
1767 } else if (attr == MediaTray.LARGE_CAPACITY) {
1768 mAttMediaTray = 11;
1769 } else if (attr == MediaTray.MAIN) {
1770 mAttMediaTray =1;
1771 } else if (attr == MediaTray.MANUAL) {
1772 mAttMediaTray = 4;
1773 } else if (attr == MediaTray.MIDDLE) {
1774 mAttMediaTray = 3;
1775 } else if (attr == MediaTray.SIDE) {
1776
1777 mAttMediaTray = 7;
1778 } else if (attr == MediaTray.TOP) {
1779 mAttMediaTray =1;
1780 } else {
1781 if (attr instanceof Win32MediaTray) {
1782 mAttMediaTray = ((Win32MediaTray)attr).winID;
1783 } else {
1784 mAttMediaTray = 1;
1785 }
1786 }
1787 }
1788
1789 private void setMediaTrayAttrib(int dmBinID) {
1790 mAttMediaTray = dmBinID;
1791 MediaTray tray = ((Win32PrintService)myService).findMediaTray(dmBinID);
1792 }
1793
1794 private int getMediaTrayAttrib() {
1795 return mAttMediaTray;
1796 }
1797
1798 private final int getSelectAttrib() {
1799 if (attributes != null) {
1800 SunPageSelection pages =
1801 (SunPageSelection)attributes.get(SunPageSelection.class);
1802 if (pages == SunPageSelection.RANGE) {
1803 return PD_PAGENUMS;
1804 } else if (pages == SunPageSelection.SELECTION) {
1805 return PD_SELECTION;
1806 } else if (pages == SunPageSelection.ALL) {
1807 return PD_ALLPAGES;
1808 }
1809 }
1810 return PD_NOSELECTION;
1811 }
1812
1813 private final boolean getPrintToFileEnabled() {
1814 SecurityManager security = System.getSecurityManager();
1815 if (security != null) {
1816 FilePermission printToFilePermission =
1817 new FilePermission("<<ALL FILES>>", "read,write");
1818 try {
1819 security.checkPermission(printToFilePermission);
1820 } catch (SecurityException e) {
1821 return false;
1822 }
1823 }
1824 return true;
1825 }
1826
1827 private final void setNativeAttributes(int flags, int fields, int values) {
1828 if (attributes == null) {
1829 return;
1830 }
1831 if ((flags & PD_PRINTTOFILE) != 0) {
1832 Destination destPrn = (Destination)attributes.get(
1833 Destination.class);
1834 if (destPrn == null) {
1835 try {
1836 attributes.add(new Destination(
1837 new File("./out.prn").toURI()));
1838 } catch (SecurityException se) {
1839 try {
1840 attributes.add(new Destination(
1841 new URI("file:out.prn")));
1842 } catch (URISyntaxException e) {
1843 }
1844 }
1845 }
1846 } else {
1847 attributes.remove(Destination.class);
1848 }
1849
1850 if ((flags & PD_COLLATE) != 0) {
1851 setCollateAttrib(SheetCollate.COLLATED, attributes);
1852 } else {
1853 setCollateAttrib(SheetCollate.UNCOLLATED, attributes);
1854 }
1855
1856 if ((flags & PD_PAGENUMS) != 0) {
1857 attributes.add(SunPageSelection.RANGE);
1858 } else if ((flags & PD_SELECTION) != 0) {
1859 attributes.add(SunPageSelection.SELECTION);
1860 } else {
1861 attributes.add(SunPageSelection.ALL);
1862 }
1863
1864 if ((fields & DM_ORIENTATION) != 0) {
1865 if ((values & SET_ORIENTATION) != 0) {
1866 setOrientAttrib(OrientationRequested.LANDSCAPE, attributes);
1867 } else {
1868 setOrientAttrib(OrientationRequested.PORTRAIT, attributes);
1869 }
1870 }
1871
1872 if ((fields & DM_COLOR) != 0) {
1873 if ((values & SET_COLOR) != 0) {
1874 setColorAttrib(Chromaticity.COLOR, attributes);
1875 } else {
1876 setColorAttrib(Chromaticity.MONOCHROME, attributes);
1877 }
1878 }
1879
1880 if ((fields & DM_PRINTQUALITY) != 0) {
1881 PrintQuality quality;
1882 if ((values & SET_RES_LOW) != 0) {
1883 quality = PrintQuality.DRAFT;
1884 } else if ((fields & SET_RES_HIGH) != 0) {
1885 quality = PrintQuality.HIGH;
1886 } else {
1887 quality = PrintQuality.NORMAL;
1888 }
1889 setQualityAttrib(quality, attributes);
1890 }
1891
1892 if ((fields & DM_DUPLEX) != 0) {
1893 Sides sides;
1894 if ((values & SET_DUP_VERTICAL) != 0) {
1895 sides = Sides.TWO_SIDED_LONG_EDGE;
1896 } else if ((values & SET_DUP_HORIZONTAL) != 0) {
1897 sides = Sides.TWO_SIDED_SHORT_EDGE;
1898 } else {
1899 sides = Sides.ONE_SIDED;
1900 }
1901 setSidesAttrib(sides, attributes);
1902 }
1903 }
1904
1905
1906
1907 private final void setResolutionDPI(int xres, int yres) {
1908 if (attributes != null) {
1909 PrinterResolution res =
1910 new PrinterResolution(xres, yres, PrinterResolution.DPI);
1911 attributes.add(res);
1912 }
1913 mAttXRes = xres;
1914 mAttYRes = yres;
1915 }
1916
1917 private void setResolutionAttrib(Attribute attr) {
1918 PrinterResolution pr = (PrinterResolution)attr;
1919 mAttXRes = pr.getCrossFeedResolution(PrinterResolution.DPI);
1920 mAttYRes = pr.getFeedResolution(PrinterResolution.DPI);
1921 }
1922
1923 private void setPrinterNameAttrib(String printerName) {
1924 PrintService service = this.getPrintService();
1925
1926 if (printerName == null) {
1927 return;
1928 }
1929
1930 if (service != null && printerName.equals(service.getName())) {
1931 return;
1932 } else {
1933 PrintService []services = PrinterJob.lookupPrintServices();
1934 for (int i=0; i<services.length; i++) {
1935 if (printerName.equals(services[i].getName())) {
1936
1937 try {
1938 this.setPrintService(services[i]);
1939 } catch (PrinterException e) {
1940 }
1941 return;
1942 }
1943 }
1944 }
1945
1946
1947 }
1948
1949 class PrintToFileErrorDialog extends Dialog implements ActionListener{
1950 public PrintToFileErrorDialog(Frame parent, String title, String message,
1951 String buttonText) {
1952 super(parent, title, true);
1953 init (parent, title, message, buttonText);
1954 }
1955
1956 public PrintToFileErrorDialog(Dialog parent, String title, String message,
1957 String buttonText) {
1958 super(parent, title, true);
1959 init (parent, title, message, buttonText);
1960 }
1961
1962 private void init(Component parent, String title, String message,
1963 String buttonText) {
1964 Panel p = new Panel();
1965 add("Center", new Label(message));
1966 Button btn = new Button(buttonText);
1967 btn.addActionListener(this);
1968 p.add(btn);
1969 add("South", p);
1970 pack();
1971
1972 Dimension dDim = getSize();
1973 if (parent != null) {
1974 Rectangle fRect = parent.getBounds();
1975 setLocation(fRect.x + ((fRect.width - dDim.width) / 2),
1976 fRect.y + ((fRect.height - dDim.height) / 2));
1977 }
1978 }
1979
1980 public void actionPerformed(ActionEvent event) {
1981 setVisible(false);
1982 dispose();
1983 return;
1984 }
1985 }
1986
1987
1988
1989
1990
1991
1992
1993 private static native void initIDs();
1994
1995 }